<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js">

<head>
    <!-- Book generated using mdBook -->
    <meta charset="UTF-8">
    <title>保龄球 - exercisms.io 快速练习</title>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="description" content="exercisms 的练习，网页版.">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="theme-color" content="#ffffff" />

    <link rel="shortcut icon" href="../favicon.png">
    <link rel="stylesheet" href="../css/variables.css">
    <link rel="stylesheet" href="../css/general.css">
    <link rel="stylesheet" href="../css/chrome.css">
    <link rel="stylesheet" href="../css/print.css" media="print">

    <!-- Fonts -->
    <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800"
        rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">

    <!-- Highlight.js Stylesheets -->
    <link rel="stylesheet" href="../highlight.css">
    <link rel="stylesheet" href="../tomorrow-night.css">
    <link rel="stylesheet" href="../ayu-highlight.css">

    <!-- Custom theme stylesheets -->
    
    <link rel="stylesheet" href="../theme/custom.css">
    

    
</head>

<body class="light">
    <!-- Provide site root to javascript -->
    <script type="text/javascript">var path_to_root = "../";</script>

    <!-- Work around some values being stored in localStorage wrapped in quotes -->
    <script type="text/javascript">
        try {
            var theme = localStorage.getItem('mdbook-theme');
            var sidebar = localStorage.getItem('mdbook-sidebar');

            if (theme.startsWith('"') && theme.endsWith('"')) {
                localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
            }

            if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
            }
        } catch (e) { }
    </script>

    <!-- Set the theme before any content is loaded, prevents flash -->
    <script type="text/javascript">
        var theme;
        try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
        if (theme === null || theme === undefined) { theme = 'light'; }
        document.body.className = theme;
        document.querySelector('html').className = theme + ' js';
    </script>

    <!-- Hide / unhide sidebar before it is displayed -->
    <script type="text/javascript">
        var html = document.querySelector('html');
        var sidebar = 'hidden';
        if (document.body.clientWidth >= 1080) {
            try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch (e) { }
            sidebar = sidebar || 'visible';
        }
        html.classList.remove('sidebar-visible');
        html.classList.add("sidebar-" + sidebar);
    </script>

    <nav id="sidebar" class="sidebar" aria-label="Table of contents">
        <ol class="chapter"><li><a href="../index.html"><strong aria-hidden="true">1.</strong> 超过 88 道练习，任点</a></li><li><a href="../low.html"><strong aria-hidden="true">2.</strong> 易</a></li><li><ol class="section"><li><a href="../hello-world/README.zh.html"><strong aria-hidden="true">2.1.</strong> hello world</a></li><li><a href="../gigasecond/README.zh.html"><strong aria-hidden="true">2.2.</strong> 千兆秒 &gt;&lt; Gigasecond)</a></li><li><a href="../leap/README.zh.html"><strong aria-hidden="true">2.3.</strong> 闰年 &gt;&lt; Leap</a></li><li><a href="../raindrops/README.zh.html"><strong aria-hidden="true">2.4.</strong> 雨滴声 &gt;&lt; Raindrops</a></li><li><a href="../reverse-string/README.zh.html"><strong aria-hidden="true">2.5.</strong> 反转字符串 &gt;&lt; Reverse String</a></li><li><a href="../nth-prime/README.zh.html"><strong aria-hidden="true">2.6.</strong> 第 n 个素数 &gt;&lt; Nth Prime</a></li><li><a href="../bob/README.zh.html"><strong aria-hidden="true">2.7.</strong> 迟钝孩子 &gt;&lt; Bob</a></li><li><a href="../beer-song/README.zh.html"><strong aria-hidden="true">2.8.</strong> 啤酒之歌 &gt;&lt; Beer Song</a></li><li><a href="../proverb/README.zh.html"><strong aria-hidden="true">2.9.</strong> 谚语串烧 &gt;&lt; Proverb</a></li><li><a href="../difference-of-squares/README.zh.html"><strong aria-hidden="true">2.10.</strong> 平方差 &gt;&lt; Difference Of Squares</a></li><li><a href="../sum-of-multiples/README.zh.html"><strong aria-hidden="true">2.11.</strong> 倍数之和 &gt;&lt; Sum Of Multiples</a></li><li><a href="../grains/README.zh.html"><strong aria-hidden="true">2.12.</strong> 谷物 &gt;&lt; Grains</a></li><li><a href="../pythagorean-triplet/README.zh.html"><strong aria-hidden="true">2.13.</strong> 勾股数 &gt;&lt; Pythagorean Triplet</a></li><li><a href="../prime-factors/README.zh.html"><strong aria-hidden="true">2.14.</strong> 素数因子 &gt;&lt; Prime Factors</a></li><li><a href="../series/README.zh.html"><strong aria-hidden="true">2.15.</strong> 子串 &gt;&lt; Series</a></li><li><a href="../armstrong-numbers/README.zh.html"><strong aria-hidden="true">2.16.</strong> 水仙花数 &gt;&lt; Armstrong Numbers</a></li><li><a href="../collatz-conjecture/README.zh.html"><strong aria-hidden="true">2.17.</strong> 3n+1 猜想 &gt;&lt; Collatz Conjecture</a></li><li><a href="../diffie-hellman/README.zh.html"><strong aria-hidden="true">2.18.</strong> 迪菲-赫尔曼密钥交换 &gt;&lt; Diffie Hellman</a></li></ol></li><li><a href="../medium.html"><strong aria-hidden="true">3.</strong> 中等</a></li><li><ol class="section"><li><a href="../saddle-points/README.zh.html"><strong aria-hidden="true">3.1.</strong> 鞍点 &gt;&lt; Saddle Points</a></li><li><a href="../isogram/README.zh.html"><strong aria-hidden="true">3.2.</strong> 等值线 &gt;&lt; Isogram</a></li><li><a href="../say/README.zh.html"><strong aria-hidden="true">3.3.</strong> 英文说数字 &gt;&lt; Say</a></li><li><a href="../run-length-encoding/README.zh.html"><strong aria-hidden="true">3.4.</strong> 游程编码 &gt;&lt; Run Length Encoding</a></li><li><a href="../isbn-verifier/README.zh.html"><strong aria-hidden="true">3.5.</strong> 图书编号 &gt;&lt; ISBN Verifier</a></li><li><a href="../perfect-numbers/README.zh.html"><strong aria-hidden="true">3.6.</strong> 数字也能分类 &gt;&lt; Perfect Numbers</a></li><li><a href="../clock/README.zh.html"><strong aria-hidden="true">3.7.</strong> 时钟 &gt;&lt; Clock</a></li><li><a href="../dot-dsl/README.zh.html"><strong aria-hidden="true">3.8.</strong> DOT DSL</a></li><li><a href="../hamming/README.zh.html"><strong aria-hidden="true">3.9.</strong> 汉明距离 &gt;&lt; Hamming</a></li><li><a href="../simple-linked-list/README.zh.html"><strong aria-hidden="true">3.10.</strong> 简单链表 &gt;&lt; Simple Linked List</a></li><li><a href="../pascals-triangle/README.zh.html"><strong aria-hidden="true">3.11.</strong> 杨辉三角形 &gt;&lt; Pascal's Triangle</a></li><li><a href="../scrabble-score/README.zh.html"><strong aria-hidden="true">3.12.</strong> 字母的分数游戏 &gt;&lt; Scrabble Score</a></li><li><a href="../pangram/README.zh.html"><strong aria-hidden="true">3.13.</strong> 全字母句 &gt;&lt; Pangram</a></li><li><a href="../paasio/README.zh.html"><strong aria-hidden="true">3.14.</strong> PaaS-IO-报告 &gt;&lt; Paasio</a></li><li><a href="../nucleotide-count/README.zh.html"><strong aria-hidden="true">3.15.</strong> 核苷酸计数 &gt;&lt; Nucleotide Count</a></li><li><a href="../luhn/README.zh.html"><strong aria-hidden="true">3.16.</strong> 模 10 算法 &gt;&lt; Luhn</a></li><li><a href="../largest-series-product/README.zh.html"><strong aria-hidden="true">3.17.</strong> 最大数字子串乘积 &gt;&lt; Largest Series Product</a></li><li><a href="../word-count/README.zh.html"><strong aria-hidden="true">3.18.</strong> 单词计数 &gt;&lt; Word Count</a></li><li><a href="../atbash-cipher/README.zh.html"><strong aria-hidden="true">3.19.</strong> Atbash 加密 &gt;&lt; Atbash Cipher</a></li><li><a href="../crypto-square/README.zh.html"><strong aria-hidden="true">3.20.</strong> 密码矩形 &gt;&lt; Crypto Square</a></li><li><a href="../rotational-cipher/README.zh.html"><strong aria-hidden="true">3.21.</strong> 旋转密码 &gt;&lt; Rotational Cipher</a></li><li><a href="../simple-cipher/README.zh.html"><strong aria-hidden="true">3.22.</strong> 简单加密 &gt;&lt; Simple Cipher</a></li><li><a href="../rail-fence-cipher/README.zh.html"><strong aria-hidden="true">3.23.</strong> 栅栏密码 &gt;&lt; Rail Fence Cipher</a></li><li><a href="../etl/README.zh.html"><strong aria-hidden="true">3.24.</strong> ETL</a></li><li><a href="../accumulate/README.zh.html"><strong aria-hidden="true">3.25.</strong> 集合操作 &gt;&lt; Accumulate</a></li><li><a href="../acronym/README.zh.html"><strong aria-hidden="true">3.26.</strong> 术语 &gt;&lt; Acronym</a></li><li><a href="../sieve/README.zh.html"><strong aria-hidden="true">3.27.</strong> 素数筛 &gt;&lt; Sieve</a></li><li><a href="../rna-transcription/README.zh.html"><strong aria-hidden="true">3.28.</strong> RNA 转录 &gt;&lt; RNA Transcription</a></li><li><a href="../triangle/README.zh.html"><strong aria-hidden="true">3.29.</strong> 三角形</a></li><li><a href="../roman-numerals/README.zh.html"><strong aria-hidden="true">3.30.</strong> 罗马数字 &gt;&lt; Roman Numerals</a></li><li><a href="../all-your-base/README.zh.html"><strong aria-hidden="true">3.31.</strong> 你所的基本</a></li><li><a href="../grade-school/README.zh.html"><strong aria-hidden="true">3.32.</strong> 学册</a></li><li><a href="../binary-search/README.zh.html"><strong aria-hidden="true">3.33.</strong> 二分查找</a></li><li><a href="../robot-simulator/README.zh.html"><strong aria-hidden="true">3.34.</strong> 机器人模拟器</a></li><li><a href="../bracket-push/README.zh.html"><strong aria-hidden="true">3.35.</strong> 括号配套</a></li><li><a href="../luhn-from/README.zh.html"><strong aria-hidden="true">3.36.</strong> Luhn From</a></li><li><a href="../queen-attack/README.zh.html"><strong aria-hidden="true">3.37.</strong> 皇后 攻击</a></li><li><a href="../bowling/README.zh.html" class="active"><strong aria-hidden="true">3.38.</strong> 保龄球</a></li><li><a href="../sublist/README.zh.html"><strong aria-hidden="true">3.39.</strong> 子列表</a></li><li><a href="../space-age/README.zh.html"><strong aria-hidden="true">3.40.</strong> 地球年</a></li><li><a href="../luhn-trait/README.zh.html"><strong aria-hidden="true">3.41.</strong> Luhn Trait</a></li><li><a href="../macros/README.zh.html"><strong aria-hidden="true">3.42.</strong> 宏</a></li><li><a href="../allergies/README.zh.html"><strong aria-hidden="true">3.43.</strong> 过敏</a></li><li><a href="../variable-length-quantity/README.zh.html"><strong aria-hidden="true">3.44.</strong> 可变长度数量</a></li><li><a href="../phone-number/README.zh.html"><strong aria-hidden="true">3.45.</strong> 电话号码</a></li><li><a href="../wordy/README.zh.html"><strong aria-hidden="true">3.46.</strong> 罗唆</a></li><li><a href="../tournament/README.zh.html"><strong aria-hidden="true">3.47.</strong> 比赛</a></li><li><a href="../custom-set/README.zh.html"><strong aria-hidden="true">3.48.</strong> 自定义 set</a></li><li><a href="../alphametics/README.zh.html"><strong aria-hidden="true">3.49.</strong> 字母谜题</a></li><li><a href="../two-bucket/README.zh.html"><strong aria-hidden="true">3.50.</strong> 两个桶</a></li><li><a href="../pig-latin/README.zh.html"><strong aria-hidden="true">3.51.</strong> 猪的拉丁文</a></li><li><a href="../diamond/README.zh.html"><strong aria-hidden="true">3.52.</strong> 钻石</a></li><li><a href="../spiral-matrix/README.zh.html"><strong aria-hidden="true">3.53.</strong> 螺旋矩阵</a></li><li><a href="../palindrome-products/README.zh.html"><strong aria-hidden="true">3.54.</strong> 回文产品</a></li><li><a href="../poker/README.zh.html"><strong aria-hidden="true">3.55.</strong> 扑克</a></li><li><a href="../grep/README.zh.html"><strong aria-hidden="true">3.56.</strong> grep</a></li><li><a href="../scale-generator/README.zh.html"><strong aria-hidden="true">3.57.</strong> 音阶生成器</a></li><li><a href="../decimal/README.zh.html"><strong aria-hidden="true">3.58.</strong> 十进制</a></li><li><a href="../anagram/README.zh.html"><strong aria-hidden="true">3.59.</strong> 字谜</a></li><li><a href="../protein-translation/README.zh.html"><strong aria-hidden="true">3.60.</strong> 蛋白质翻译</a></li><li><a href="../robot-name/README.zh.html"><strong aria-hidden="true">3.61.</strong> 机器人名称</a></li><li><a href="../book-store/README.zh.html"><strong aria-hidden="true">3.62.</strong> 书店</a></li></ol></li><li><a href="../high.html"><strong aria-hidden="true">4.</strong> 难</a></li><li><ol class="section"><li><a href="../ocr-numbers/README.zh.html"><strong aria-hidden="true">4.1.</strong> OCR 号码</a></li><li><a href="../minesweeper/README.zh.html"><strong aria-hidden="true">4.2.</strong> 扫雷</a></li><li><a href="../dominoes/README.zh.html"><strong aria-hidden="true">4.3.</strong> 骨牌</a></li><li><a href="../parallel-letter-frequency/README.zh.html"><strong aria-hidden="true">4.4.</strong> 并行字母频率</a></li><li><a href="../rectangles/README.zh.html"><strong aria-hidden="true">4.5.</strong> 矩形</a></li><li><a href="../forth/README.zh.html"><strong aria-hidden="true">4.6.</strong> Forth</a></li><li><a href="../circular-buffer/README.zh.html"><strong aria-hidden="true">4.7.</strong> 循环缓冲区</a></li><li><a href="../react/README.zh.html"><strong aria-hidden="true">4.8.</strong> React</a></li></ol></li><li><a href="../untag.html"><strong aria-hidden="true">5.</strong> 未标签</a></li><li><ol class="section"><li><a href="../hexadecimal/README.zh.html"><strong aria-hidden="true">5.1.</strong> 十六进制</a></li><li><a href="../nucleotide-codons/README.zh.html"><strong aria-hidden="true">5.2.</strong> 核苷酸密码子</a></li><li><a href="../two-fer/README.zh.html"><strong aria-hidden="true">5.3.</strong> two-fer</a></li><li class="spacer"></li></ol></li><li><a href="../add-test-code.html">修改 mdBook 主题</a></li></ol>
    </nav>

    <div id="page-wrapper" class="page-wrapper">

        <div class="page">
            
            <div id="menu-bar" class="menu-bar">
                <div id="menu-bar-sticky-container">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents"
                            aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <!-- START - Rust Cookbook customization -->
                        <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit"
                            aria-haspopup="true" aria-expanded="false" aria-controls="edit">
                            <i class="fa fa-edit">Edit</i>
                        </button>
                        <!-- END - Rust Cookbook customization -->
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme"
                            aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)"
                            aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                        
                    </div>

                    <h1 class="menu-title">exercisms.io 快速练习</h1>
                    
                        <div class="right-buttons">
                            <a href="../print.html" title="Print this book" aria-label="Print this book">
                                <i id="print-button" class="fa fa-print"></i>
                            </a>
                            
                        </div>
                    </div>
                </div>

            
            <div id="search-wrapper" class="hidden">
                <form id="searchbar-outer" class="searchbar-outer">
                    <input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..."
                        aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                </form>
                <div id="searchresults-outer" class="searchresults-outer hidden">
                    <div id="searchresults-header" class="searchresults-header"></div>
                    <ul id="searchresults">
                    </ul>
                </div>
            </div>
            

            <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
            <script type="text/javascript">
                document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                Array.from(document.querySelectorAll('#sidebar a')).forEach(function (link) {
                    link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                });
            </script>

            <!-- // START - Rust Cookbook customization -->
            <script>
                document.getElementById("edit-button").addEventListener("click", function () {
                    var editWindow = window.open("https://github.com/chinanf-boy/exercism-rust-zh/edit/master/src/bowling/README.zh.md");
                });</script>
            <!-- // END - Rust Cookbook customization -->

            <div id="content" class="content">
                <main>
                    <a class="header" href="#bowling" id="bowling"><h1>Bowling</h1></a>
<a class="header" href="#1-readme" id="1-readme"><h2>1. Readme</h2></a>
<a class="header" href="#保龄球" id="保龄球"><h1>保龄球</h1></a>
<p>打保龄球比赛。</p>
<p>保龄球是一种游戏，玩家掷出一个沉重的球来击倒排列成三角形的罐子。编写代码以跟踪保龄球比赛的得分。</p>
<a class="header" href="#保龄球得分" id="保龄球得分"><h2>保龄球得分</h2></a>
<p>游戏由 10 轮组成。一轮由一个或两个球投掷组成，其中 10 个罐子处于该轮初始化状态。一个回合有三种情况。</p>
<ul>
<li>
<p>开放(open)轮是轮的记录小于 10 的分数。在这种情况下，该轮的分数是被击倒数。</p>
</li>
<li>
<p>备用(spare)的是所有十个罐子被第二次投掷击倒。备用的总分是 10 加上，下一次投掷击倒的罐子数量。</p>
</li>
<li>
<p>全倒(strike)是所有十个罐子被第一次击倒。全倒的总分是 10 加上，在接下来的两次投掷击倒的罐子数量。如果进行第二次击球又触发 strike，则再扔球之前不确定第一次击球的值。</p>
</li>
</ul>
<p>这是一个三轮的例子:</p>
<table><thead><tr><th align="center">  第 1 轮  </th><th align="center">  第 2 轮   </th><th align="center">  第 3 轮  </th></tr></thead><tbody>
<tr><td align="center"> X(strike) </td><td align="center"> 5 /(spare) </td><td align="center"> 9 0(open) </td></tr>
</tbody></table>
<p>第 1 轮是(10 + 5 + 5)= 20</p>
<p>第 2 轮是(5 + 5 + 9)= 19</p>
<p>轮 3 是(9 + 0)= 9</p>
<p>这意味着当前的运行总数为 48.</p>
<p>游戏中的第十轮是一个特例。如果有人投掷全倒或备用，那么他们会得到一个补球。补球会计入第 10 轮的总和。在补球上获得一次全倒或备用不会给球员带来更多的补球。第 10 轮的总分是被击倒的罐子总数。</p>
<p>对于 X1 /(全倒和备用)的第十轮，总分为 20。</p>
<p>对于 XXX (三次全倒)的第十轮，总分为 30。</p>
<a class="header" href="#要求" id="要求"><h2>要求</h2></a>
<p>编写代码以跟踪保龄球比赛的得分。它应该支持两个操作:</p>
<ul>
<li><code>roll(pins : int)</code>每次玩家滚球时都会调用。这个参数是被击倒的罐子数量.</li>
<li><code>score() : int</code>仅在游戏结束时才会被调用。它返回该游戏的总分。</li>
</ul>
<a class="header" href="#资源" id="资源"><h2>资源</h2></a>
<p>保龄球比赛， Kata 的 UncleBob<a href="http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata">http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata</a></p>
<a class="header" href="#2-开始你的表演" id="2-开始你的表演"><h2>2. 开始你的表演</h2></a>
<pre><pre class="playpen"><code class="language-rust editable">#[derive(Debug, PartialEq)]
pub enum Error {
   NotEnoughPinsLeft,
   GameComplete,
}

pub struct BowlingGame {}

impl BowlingGame {
   pub fn new() -&gt; Self {
       unimplemented!();
   }

   pub fn roll(&amp;mut self, pins: u16) -&gt; Result&lt;(), Error&gt; {
       unimplemented!(&quot;Record that {} pins have been scored&quot;, pins);
   }

   pub fn score(&amp;self) -&gt; Option&lt;u16&gt; {
       unimplemented!(&quot;Return the score if the game is complete, or None if not.&quot;);
   }
}

</code></pre></pre>
<a class="header" href="#3-测试代码查看" id="3-测试代码查看"><h2>3. 测试代码查看</h2></a>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[test]
fn roll_returns_a_result() {
   let mut game = BowlingGame::new();
   assert!(game.roll(0).is_ok());
}

#[test]
//#[ignore]
fn you_can_not_roll_more_than_ten_pins_in_a_single_roll() {
   let mut game = BowlingGame::new();

   assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn a_game_score_is_some_if_ten_frames_have_been_rolled() {
   let mut game = BowlingGame::new();

   for _ in 0..10 {
       let _ = game.roll(0);
       let _ = game.roll(0);
   }

   assert!(game.score().is_some());
}

#[test]
//#[ignore]
fn you_can_not_score_a_game_with_no_rolls() {
   let game = BowlingGame::new();

   assert_eq!(game.score(), None);
}

#[test]
//#[ignore]
fn a_game_score_is_none_if_fewer_than_ten_frames_have_been_rolled() {
   let mut game = BowlingGame::new();

   for _ in 0..9 {
       let _ = game.roll(0);
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), None);
}

#[test]
//#[ignore]
fn a_roll_is_err_if_the_game_is_done() {
   let mut game = BowlingGame::new();

   for _ in 0..10 {
       let _ = game.roll(0);
       let _ = game.roll(0);
   }

   assert_eq!(game.roll(0), Err(Error::GameComplete));;
}

#[test]
//#[ignore]
fn twenty_zero_pin_rolls_scores_zero() {
   let mut game = BowlingGame::new();

   for _ in 0..20 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(0));
}

#[test]
//#[ignore]
fn ten_frames_without_a_strike_or_spare() {
   let mut game = BowlingGame::new();

   for _ in 0..10 {
       let _ = game.roll(3);
       let _ = game.roll(6);
   }

   assert_eq!(game.score(), Some(90));
}

#[test]
//#[ignore]
fn spare_in_the_first_frame_followed_by_zeros() {
   let mut game = BowlingGame::new();

   let _ = game.roll(6);
   let _ = game.roll(4);

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(10));
}

#[test]
//#[ignore]
fn points_scored_in_the_roll_after_a_spare_are_counted_twice_as_a_bonus() {
   let mut game = BowlingGame::new();

   let _ = game.roll(6);
   let _ = game.roll(4);
   let _ = game.roll(3);

   for _ in 0..17 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(16));
}

#[test]
//#[ignore]
fn consecutive_spares_each_get_a_one_roll_bonus() {
   let mut game = BowlingGame::new();

   let _ = game.roll(5);
   let _ = game.roll(5);
   let _ = game.roll(3);
   let _ = game.roll(7);
   let _ = game.roll(4);

   for _ in 0..15 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(31));
}

#[test]
//#[ignore]
fn if_the_last_frame_is_a_spare_you_get_one_extra_roll_that_is_scored_once() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(5);
   let _ = game.roll(5);
   let _ = game.roll(7);

   assert_eq!(game.score(), Some(17));
}

#[test]
//#[ignore]
fn a_strike_earns_ten_points_in_a_frame_with_a_single_roll() {
   let mut game = BowlingGame::new();

   let _ = game.roll(10);

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(10));
}

#[test]
//#[ignore]
fn points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus() {
   let mut game = BowlingGame::new();

   let _ = game.roll(10);
   let _ = game.roll(5);
   let _ = game.roll(3);

   for _ in 0..16 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(26));
}

#[test]
//#[ignore]
fn consecutive_strikes_each_get_the_two_roll_bonus() {
   let mut game = BowlingGame::new();

   let _ = game.roll(10);
   let _ = game.roll(10);
   let _ = game.roll(10);
   let _ = game.roll(5);
   let _ = game.roll(3);

   for _ in 0..12 {
       let _ = game.roll(0);
   }

   assert_eq!(game.score(), Some(81));
}

#[test]
//#[ignore]
fn a_strike_in_the_last_frame_earns_a_two_roll_bonus_that_is_counted_once() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);
   let _ = game.roll(7);
   let _ = game.roll(1);

   assert_eq!(game.score(), Some(18));
}

#[test]
//#[ignore]
fn a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);
   let _ = game.roll(7);
   let _ = game.roll(3);

   assert_eq!(game.score(), Some(20));
}

#[test]
//#[ignore]
fn strikes_with_the_two_roll_bonus_do_not_get_a_bonus_roll() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);
   let _ = game.roll(10);
   let _ = game.roll(10);

   assert_eq!(game.score(), Some(30));
}

#[test]
//#[ignore]
fn a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(7);
   let _ = game.roll(3);
   let _ = game.roll(10);

   assert_eq!(game.score(), Some(20));
}

#[test]
//#[ignore]
fn all_strikes_is_a_perfect_score_of_300() {
   let mut game = BowlingGame::new();

   for _ in 0..12 {
       let _ = game.roll(10);
   }

   assert_eq!(game.score(), Some(300));
}

#[test]
//#[ignore]
fn you_can_not_roll_more_than_ten_pins_in_a_single_frame() {
   let mut game = BowlingGame::new();

   assert!(game.roll(5).is_ok());
   assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn first_bonus_ball_after_a_final_strike_can_not_score_an_invalid_number_of_pins() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn the_two_balls_after_a_final_strike_can_not_score_an_invalid_number_of_pins() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert!(game.roll(5).is_ok());
   assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn the_two_balls_after_a_final_strike_can_be_a_strike_and_non_strike() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert!(game.roll(10).is_ok());
   assert!(game.roll(6).is_ok());
}

#[test]
//#[ignore]
fn the_two_balls_after_a_final_strike_can_not_be_a_non_strike_followed_by_a_strike() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert!(game.roll(6).is_ok());
   assert_eq!(game.roll(10), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn second_bonus_ball_after_a_final_strike_can_not_score_an_invalid_number_of_pins_even_if_first_is_strike(
) {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert!(game.roll(10).is_ok());
   assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));;
}

#[test]
//#[ignore]
fn if_the_last_frame_is_a_strike_you_can_not_score_before_the_extra_rolls_are_taken() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(10);

   assert_eq!(game.score(), None);

   let _ = game.roll(10);

   assert_eq!(game.score(), None);

   let _ = game.roll(10);

   assert!(game.score().is_some());
}

#[test]
//#[ignore]
fn if_the_last_frame_is_a_spare_you_can_not_create_a_score_before_extra_roll_is_taken() {
   let mut game = BowlingGame::new();

   for _ in 0..18 {
       let _ = game.roll(0);
   }

   let _ = game.roll(5);
   let _ = game.roll(5);

   assert_eq!(game.score(), None);

   let _ = game.roll(10);

   assert!(game.score().is_some());
}

#}</code></pre></pre>
<a class="header" href="#4-答案" id="4-答案"><h2>4. 答案</h2></a>
<p><details></p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[derive(Debug, PartialEq)]
pub enum Error {
   NotEnoughPinsLeft,
   GameComplete,
}

pub struct BowlingGame {
   frames: Vec&lt;Frame&gt;,
}

struct Frame {
   rolls: Vec&lt;u16&gt;,
   bonus: Vec&lt;u16&gt;,
}

impl Frame {
   fn score(&amp;self) -&gt; u16 {
       self.roll_score() + self.bonus_score()
   }

   fn roll_score(&amp;self) -&gt; u16 {
       self.rolls.iter().sum()
   }

   fn bonus_score(&amp;self) -&gt; u16 {
       self.bonus.iter().sum()
   }

   fn is_valid(&amp;self) -&gt; bool {
       self.rolls_valid() &amp;&amp; self.bonus_valid()
   }

   fn rolls_valid(&amp;self) -&gt; bool {
       self.roll_score() &lt;= 10
   }

   fn bonus_valid(&amp;self) -&gt; bool {
       if self.is_open() || !self.bonus_done() {
           return true;
       }

       if self.is_spare() {
           return self.bonus_score() &lt;= 10;
       }

       if let Some(first) = self.bonus.iter().next() {
           if *first == 10 {
               self.bonus_score() &lt;= 20
           } else {
               self.bonus_score() &lt;= 10
           }
       } else {
           unreachable!();
       }
   }

   fn is_complete(&amp;self) -&gt; bool {
       self.is_open() || self.bonus_done()
   }

   fn rolls_done(&amp;self) -&gt; bool {
       self.rolls.len() == 2 || self.is_strike()
   }

   fn bonus_done(&amp;self) -&gt; bool {
       (self.is_spare() &amp;&amp; self.bonus.len() == 1) || (self.is_strike() &amp;&amp; self.bonus.len() == 2)
   }

   fn is_open(&amp;self) -&gt; bool {
       self.rolls.len() == 2 &amp;&amp; self.roll_score() &lt; 10
   }

   fn is_spare(&amp;self) -&gt; bool {
       self.rolls.len() == 2 &amp;&amp; self.roll_score() == 10
   }

   fn is_strike(&amp;self) -&gt; bool {
       self.rolls.len() == 1 &amp;&amp; self.roll_score() == 10
   }

   fn add_roll(&amp;mut self, roll: u16) {
       if !self.is_complete() {
           if self.is_spare() || self.is_strike() {
               self.bonus.push(roll)
           } else {
               self.rolls.push(roll)
           }
       }
   }

   fn new() -&gt; Self {
       Frame {
           rolls: vec![],
           bonus: vec![],
       }
   }
}

impl BowlingGame {
   pub fn new() -&gt; Self {
       BowlingGame {
           frames: vec![Frame::new()],
       }
   }

   pub fn roll(&amp;mut self, pins: u16) -&gt; Result&lt;(), Error&gt; {
       if pins &gt; 10 {
           Err(Error::NotEnoughPinsLeft)
       } else {
           if self.score().is_some() {
               return Err(Error::GameComplete);
           }

           for frame in self.frames.iter_mut() {
               frame.add_roll(pins)
           }

           if self.frames.iter().any(|f| !f.is_valid()) {
               return Err(Error::NotEnoughPinsLeft);
           }

           if self.frames.iter().last().unwrap().rolls_done() &amp;&amp; self.frames.len() &lt; 10 {
               self.frames.push(Frame::new());
           }

           Ok(())
       }
   }

   pub fn score(&amp;self) -&gt; Option&lt;u16&gt; {
       if !self.is_done() {
           None
       } else {
           Some(self.frames.iter().fold(0, |acc, r| acc + r.score()))
       }
   }

   fn is_done(&amp;self) -&gt; bool {
       self.frames.len() == 10 &amp;&amp; self.frames.iter().all(|f| f.is_complete())
   }
}

#}</code></pre></pre>
<p></details></p>
<hr />
<hr />
<a class="header" href="#填充相关" id="填充相关"><h2>填充/相关</h2></a>

                </main>

                <nav class="nav-wrapper" aria-label="Page navigation">
                    <!-- Mobile navigation buttons -->
                    
                    <a rel="prev" href="../queen-attack/README.zh.html" class="mobile-nav-chapters previous" title="Previous chapter"
                        aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                    

                    
                    <a rel="next" href="../sublist/README.zh.html" class="mobile-nav-chapters next" title="Next chapter"
                        aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
                    

                    <div style="clear: both"></div>
                </nav>
            </div>
        </div>

        <nav class="nav-wide-wrapper" aria-label="Page navigation">
            
            <a href="../queen-attack/README.zh.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter"
                aria-keyshortcuts="Left">
                <i class="fa fa-angle-left"></i>
            </a>
            

            
            <a href="../sublist/README.zh.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter"
                aria-keyshortcuts="Right">
                <i class="fa fa-angle-right"></i>
            </a>
            
        </nav>

    </div>

    

    
    <!-- Google Analytics Tag -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-128555056-1"></script>
    
    <script type="text/javascript">
        var localAddrs = ["localhost", "127.0.0.1", ""];
        if (localAddrs.indexOf(document.location.hostname) === -1) {
            window.dataLayer = window.dataLayer || [];
            function gtag() { dataLayer.push(arguments); }
            gtag('js', new Date());

            gtag('config', 'UA-128555056-1');
        }
    </script>
    

    
    <script src="../ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="../editor.js" type="text/javascript" charset="utf-8"></script>
    <script src="../mode-rust.js" type="text/javascript" charset="utf-8"></script>
    <script src="../theme-dawn.js" type="text/javascript" charset="utf-8"></script>
    <script src="../theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
    

    
    <script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
    

    <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
    <script src="../book.js" type="text/javascript" charset="utf-8"></script>

    <!-- Custom JS scripts -->
    

    

</body>

</html>